<template>
  <div class="page-content">
    <cc-table :listData="dataList" :listCount="dataCount" @selection-change="selectionChange" v-bind="contentConfig" v-model:page="pageInfo">
      <!-- 表格 header 插槽 -->
      <template #headerHandler>
        <el-button @click="handleNew" type="primary" size="mini" icon="el-icon-plus">新增</el-button>
        <el-button @click="handleNew" type="default" size="mini" icon="el-icon-refresh">刷新</el-button>
      </template>
      <!-- 表格列插槽 -->
      <template #status="scope">
        <!-- <span>{{ scope.row.enable ? '✔' : '×' }}</span> -->
        <el-button plain size="mini" :type="scope.row.enable ? 'success' : 'danger'">{{ scope.row.enable ? '启用' : '禁用' }}</el-button>
      </template>
      <template #createAt="scope">
        <strong>{{ $filters.formatTime(scope.row.createAt) }}</strong>
      </template>
      <template #handler="scope">
        <el-button size="mini" type="text" icon="el-icon-edit">编辑</el-button>
        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteClick(scope.row)">删除</el-button>
      </template>
      <!-- <template #image="scope">
        <el-image style="width: 60px; height: 60px" :src="scope.row.img_url" :preview-src-list="[scope.row.img_url]"></el-image>
      </template> -->

      <!-- 2021年10月5日 23:15:15 chanchaw 动态插入其他所有插槽 -->
      <template v-for="item in otherSlots" :key="item.prop" #[item.slotName]="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </cc-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { CcTable } from '@/base-ui/table'
import { ElButton, ElImage } from 'element-plus'
import { useStore } from '@/store'

export default defineComponent({
  name: 'PageContent',
  components: { CcTable, ElButton, ElImage },
  props: {
    pageName: {
      type: String,
      required: true
    },
    contentConfig: {
      type: Object,
      required: true
    },
    vuexModuleName: {
      type: String,
      required: true
    }
  },
  setup(props) {
    const store = useStore()

    const pageInfo = ref({ currentPage: 1, pageSize: 10 })

    watch(pageInfo, () => getPageData())

    const getPageListActionUrl = `${props.vuexModuleName}/getPageListAction`
    console.log('请求数据的url：', getPageListActionUrl)
    const getPageData = (queryInfo: any = {}) => {
      store.dispatch(getPageListActionUrl, {
        // store.dispatch('system/getPageListAction', {
        // 执行项目目录：src/store/main/system 文件中的方法 getPageListAction
        pageName: props.pageName,
        queryInfo: {
          offset: pageInfo.value.pageSize * (pageInfo.value.currentPage - 1),
          count: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }

    getPageData()

    const dataUrl = `${props.vuexModuleName}/pageListData`
    const countUrl = `${props.vuexModuleName}/pageListCount`

    const dataList = computed(() => store.getters[dataUrl](props.pageName))
    const dataCount = computed(() => store.getters[countUrl](props.pageName))

    const selectionChange = (value: any) => {
      console.log(value)
    }

    // 获取所有其他插槽
    const otherSlots = props.contentConfig.propList.filter((item: any) => {
      if (item.slotName === 'status') return false
      if (item.slotName === 'createAt') return false
      if (item.slotName === 'updateAt') return false
      if (item.slotName === 'handler') return false
      // if (item.slotName === 'image') return false
      return true
    })

    // 删除一行数据
    const handleDeleteClick = (row: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: row.id
      })
    }

    return { dataList, dataCount, selectionChange, getPageData, pageInfo, otherSlots, handleDeleteClick }
  }
})
</script>
<style scoped lang="less">
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
